package com.dpf.hou.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dpf.hou.mapper.AdoptionMapper;
import com.dpf.hou.mapper.PetMapper;
import com.dpf.hou.pojo.Adoption;
import com.dpf.hou.pojo.Pet;
import com.dpf.hou.pojo.User;
import com.dpf.hou.service.AdoptionService;
import com.dpf.hou.util.RefuseUtil;
import com.dpf.hou.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdoptionServiceImp implements AdoptionService {
    @Autowired
    private AdoptionMapper adoptionMapper;
    @Autowired
    private PetMapper petMapper;
    @Override
    public Page<Adoption> getPageByName(int page, int size, String username) {
        QueryWrapper<Adoption> q=new QueryWrapper<>();
        if(username!=null && !"".equals(username)){
            q.like("username",username);
        }

        Page<Adoption> p=new Page<>(page,size);
        return adoptionMapper.selectPage(p,q);
    }

    @Override
    public boolean petApply(int userid,int petid,String note) {
        QueryWrapper<Pet> q=new QueryWrapper<>();
        Date d=new Date();
        Adoption a=new Adoption(0,userid,petid,d,1,note);
        int rows=adoptionMapper.insert(a);
        return rows==0?false:true;
    }

    @Override
    public Page<Adoption> adoGet(int page, int size) {
        QueryWrapper<Adoption> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("adostatus",1);
        Page<Adoption> p=new Page<>(page,size);
        return adoptionMapper.selectPage(p,null);
    }

    @Override
    public boolean petAllow(Adoption adoption) {
        Date d=new Date();
        adoption.setAdostatus(2);
        adoption.setDate(d);
        QueryWrapper<Adoption> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("adoid",adoption.getAdoid());
        int rows=adoptionMapper.update(adoption,queryWrapper);
        return rows == 0? false:true;
    }

    @Override
    public boolean delAdo(Integer adoid) {

        RefuseUtil result=null;
        QueryWrapper<Adoption> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("adoid",adoid);
        Adoption a=adoptionMapper.selectOne(queryWrapper);
        int rows= adoptionMapper.deleteById(adoid);
//        if(rows==0){
//            result=new RefuseUtil(false,"拒绝失败",a.getPetid());
//        }else {
//            result=new RefuseUtil(true,"拒绝成功",a.getPetid());
//        }

        return rows==0?false:true;
//        return result;
    }

    @Override
    public RefuseUtil refuseAdo(Integer adoid) {
        RefuseUtil result=null;
        QueryWrapper<Adoption> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("adoid",adoid);
        Adoption a=adoptionMapper.selectOne(queryWrapper);
        a.setAdostatus(3);
        int rows= adoptionMapper.update(a,queryWrapper);
        if(rows==0){
            result=new RefuseUtil(false,"拒绝失败",a.getPetid());
        }else {
            result=new RefuseUtil(true,"拒绝成功",a.getPetid());
        }

//        return rows==0?false:true;
        return result;
    }

    @Override
    public Page<Adoption> adoRecordGet(int page, int size,int userid) {
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",userid);
        Page<Adoption> p=new Page<>(page,size);
        return adoptionMapper.selectPage(p,queryWrapper);
    }
}
